home *** CD-ROM | disk | FTP | other *** search
/ Merciful 2 / Merciful - Disc 2.iso / software / t / tinbinhackerutil's22.dms / tinbinhackerutil's22.adf / CacheControl / cc.c < prev    next >
C/C++ Source or Header  |  1995-03-28  |  9KB  |  360 lines

  1. /* CC.c */
  2.  
  3. /* Cachebits zeile 189 ff */
  4.  
  5. #include "sc:source/cc/cc-INCLUDES.h"
  6.  
  7. /* Version | Revisionscontrol */
  8.  
  9. #define    VERSION        1
  10. #define    REVISION    20
  11. #define    DATE        "__Date__"
  12. #define    VERS        "CacheControl 1.20"
  13. #define    VSTRING        "CacheControl 1.20 ("__DATE__")\n\r"
  14. #define    VERSTAG        "\0$VER: CacheControl 1.20 ("__DATE__")"
  15.  
  16. void GetSysLevel(void);
  17. void OpenLibs(void);
  18. void OpenGUI(void);
  19. void CloseAll(void);
  20. void LoadPrefs(char *name);
  21. void SavePrefs(char *name);
  22.  
  23. /* Externs */
  24.  
  25. extern struct ExecBase         *SysBase;
  26.  
  27. /* Globals */
  28.  
  29. UBYTE VersTag[]=VERSTAG;        /* Versionsstring */
  30. struct IntuitionBase    *IntuitionBase=0l;
  31. struct GfxBase             *GfxBase=0l;
  32. struct Library             *UtilityBase=0l,*DiskfontBase=0l;
  33. struct Screen            *scr=0l;
  34. struct Window            *win=0l;
  35. struct Gadget            *GList=0l;
  36. struct TextAttr         *sysfont=0l;
  37. ULONG oldbits,cachebits,cachemask;
  38. UWORD syslevel;
  39.  
  40. struct TagItem wintags[]={
  41.     WA_Left,                50,
  42.     WA_Top,                    20,
  43.     WA_InnerWidth,            200,
  44.     WA_InnerHeight,            70,
  45.     WA_IDCMP,                IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|BUTTONIDCMP,
  46.     WA_Flags,                WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_CLOSEGADGET|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_GIMMEZEROZERO|WFLG_RMBTRAP,
  47.     WA_Title,                (ULONG *)"CacheControl V1.20",
  48.     WA_ScreenTitle,            (ULONG *)"CacheControl V1.20 (C) 1995 by ENSONIC of TRINOMIC",
  49.     WA_CustomScreen,        0l,
  50.     WA_Gadgets,                0l,
  51.     WA_PubScreenName,        0l,
  52.     WA_PubScreenFallBack,    TRUE,
  53.     TAG_DONE
  54. };
  55.  
  56. UBYTE *DCLabels[]={"DCache Off","DCache On",0l };
  57. UBYTE *DBLabels[]={"DBurst Off","DBurst On",0l };
  58. UBYTE *ICLabels[]={"ICache Off","ICache On",0l };
  59. UBYTE *IBLabels[]={"IBurst Off","IBurst On",0l };
  60. UBYTE *CBLabels[]={"CopyBack Off","CopyBack On",0l };
  61.  
  62. Prefs_Hdr        prefs_hdr;
  63. Data_ICache        d_i;
  64. Data_DCache        d_d;
  65. Data_CpyBck        d_c;
  66.  
  67. void GetSysLevel(void)
  68. {
  69.     UWORD level;
  70.  
  71.     syslevel=0;
  72.     level=SysBase->AttnFlags;
  73.     if(level&(1L<<1)) syslevel=1;
  74.     if(level&(1L<<2)) syslevel=2;
  75.     if(level&(1L<<3)) syslevel=3;
  76.     if(!syslevel) exit(1);
  77. }
  78.  
  79. void OpenLibs(void)
  80. {
  81.     if(!(IntuitionBase=OpenLibrary("intuition.library",37))) CloseAll();
  82.     if(!(GfxBase=OpenLibrary("graphics.library",37))) CloseAll();
  83.     if(!(UtilityBase=OpenLibrary("utility.library",37))) CloseAll();
  84. }
  85.  
  86. void OpenGUI(void)
  87. {
  88.     WORD winx,winy,fonty,gwidth,cgwi;
  89.     struct NewGadget ng;
  90.     struct Gadget *g;
  91.     APTR VisualInfo=0l;
  92.     struct RastPort *rp;
  93.  
  94.     oldbits=CacheControl(0l,0l);
  95.     d_i.cache=d_i.burst=d_d.cache=d_d.burst=d_c.cpybck=0;
  96.     if(oldbits&(1L<< 0)) d_i.cache=1;
  97.     if(oldbits&(1L<< 4)) d_i.burst=1;
  98.     if(oldbits&(1L<< 8)) d_d.cache=1;
  99.     if(oldbits&(1L<<12)) d_d.burst=1;
  100.     if(oldbits&(1L<<31)) d_c.cpybck=1;
  101.  
  102.     if(!(scr=LockPubScreen(0l))) CloseAll();
  103.     sysfont=scr->Font;
  104.     rp=&scr->RastPort;
  105.     fonty=max(11,(sysfont->ta_YSize+4));
  106.     winy=((syslevel+1)*fonty)+10;
  107.     gwidth=TextLength(rp,CBLabels[0],strlen(CBLabels[0]))+30;
  108.     cgwi=(gwidth<<1)/3-1;
  109.     winx=gwidth+gwidth+10;
  110.  
  111.     if(!(VisualInfo=GetVisualInfo(scr,TAG_DONE))) CloseAll();
  112.  
  113.     if(!(g=CreateContext(&GList))) CloseAll();
  114.  
  115.     ng.ng_LeftEdge        =4;
  116.     ng.ng_TopEdge        =6+syslevel*(fonty+1);
  117.     ng.ng_Width            =cgwi;
  118.     ng.ng_Height        =fonty;
  119.     ng.ng_GadgetText    ="Save";
  120.     ng.ng_TextAttr        =sysfont;
  121.     ng.ng_GadgetID        =0;
  122.     ng.ng_Flags            =PLACETEXT_IN;
  123.     ng.ng_VisualInfo    =VisualInfo;
  124.     g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE);
  125.  
  126.     ng.ng_LeftEdge        =5+(cgwi+1);
  127.     ng.ng_GadgetText    ="Use";
  128.     ng.ng_GadgetID        =1;
  129.     g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE);
  130.  
  131.     ng.ng_LeftEdge        =7+(cgwi+1)+(cgwi+1);
  132.     ng.ng_GadgetText    ="Cancel";
  133.     ng.ng_GadgetID        =2;
  134.     g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE);
  135.  
  136.     if(syslevel>0)
  137.     {
  138.         ng.ng_LeftEdge        =4;
  139.         ng.ng_TopEdge        =2;
  140.         ng.ng_Width            =gwidth;
  141.         ng.ng_GadgetText    ="";
  142.         ng.ng_GadgetID        =10;
  143.         ng.ng_Flags            =PLACETEXT_LEFT;
  144.         g=CreateGadget(CYCLE_KIND,g,&ng,GTCY_Labels,&ICLabels,GTCY_Active,d_i.cache,TAG_DONE);
  145.  
  146.         ng.ng_LeftEdge        =gwidth+6;
  147.         ng.ng_GadgetID        =11;
  148.         g=CreateGadget(CYCLE_KIND,g,&ng,GTCY_Labels,&IBLabels,GTCY_Active,d_i.burst,TAG_DONE);
  149.     }
  150.     if(syslevel>1)
  151.     {
  152.         ng.ng_LeftEdge        =4;
  153.         ng.ng_TopEdge        =2+(fonty+1);
  154.         ng.ng_GadgetID        =12;
  155.         g=CreateGadget(CYCLE_KIND,g,&ng,GTCY_Labels,&DCLabels,GTCY_Active,d_d.cache,TAG_DONE);
  156.  
  157.         ng.ng_LeftEdge        =gwidth+6;
  158.         ng.ng_GadgetID        =13;
  159.         g=CreateGadget(CYCLE_KIND,g,&ng,GTCY_Labels,&DBLabels,GTCY_Active,d_d.burst,TAG_DONE);
  160.     }
  161.     if(syslevel>2)
  162.     {
  163.         ng.ng_LeftEdge        =4;
  164.         ng.ng_TopEdge        =2+(fonty+1)+(fonty+1);
  165.         ng.ng_GadgetID        =14;
  166.         g=CreateGadget(CYCLE_KIND,g,&ng,GTCY_Labels,&CBLabels,GTCY_Active,d_c.cpybck,TAG_DONE);
  167.     }
  168.  
  169.     wintags[2].ti_Data=winx;
  170.     wintags[3].ti_Data=winy;
  171.     wintags[8].ti_Data=scr;
  172.     wintags[9].ti_Data=GList;
  173.     if(!(win=OpenWindowTagList(0l,wintags))) CloseAll();
  174.  
  175.     UnlockPubScreen(0l,scr);
  176. }
  177.  
  178. void CloseAll(void)
  179. {
  180.     if(win)                CloseWindow(win);
  181.     if(UtilityBase)        CloseLibrary(UtilityBase);
  182.     if(GfxBase)            CloseLibrary(GfxBase);
  183.     if(IntuitionBase)    CloseLibrary(IntuitionBase);
  184.     exit(1);
  185. }
  186.  
  187. void LoadPrefs(char *name)
  188. {
  189.     FILE *prefs;
  190.     ULONG subtyp,len;
  191.  
  192.     if(prefs=fopen(name,"rb"))
  193.     {
  194.         if(!fread(&prefs_hdr,8,1,prefs)) { fclose(prefs);return; }
  195.         if(prefs_hdr.id!=PREF_FORM) { fclose(prefs);return; }
  196.         len=prefs_hdr.len;
  197.         if(!fread(&subtyp,4,1,prefs)) { fclose(prefs);return; }
  198.         if(subtyp!=PREF_PREF) { fclose(prefs);return; }
  199.  
  200.         while(len>0)
  201.         {
  202.             if(!fread(&prefs_hdr,8,1,prefs)) { fclose(prefs);return; }
  203.             len-=8;
  204.             switch(prefs_hdr.id)
  205.             {
  206.                 case PREF_ICAC:
  207.                     if(!fread(&d_i,sizeof(d_i),1,prefs)) { fclose(prefs);return; }
  208.                     break;
  209.                 case PREF_DCAC:
  210.                     if(!fread(&d_d,sizeof(d_d),1,prefs)) { fclose(prefs);return; }
  211.                     break;
  212.                 case PREF_CBCK:
  213.                     if(!fread(&d_c,sizeof(d_c),1,prefs)) { fclose(prefs);return; }
  214.                     break;
  215.                 default:
  216.                     fseek(prefs,prefs_hdr.len,1);
  217.                     break;
  218.             }
  219.             len-=prefs_hdr.len;
  220.             if(prefs_hdr.len&1) { len--;fseek(prefs,1L,1); }
  221.         }
  222.         fclose(prefs);
  223.     }
  224. }
  225.  
  226. void SavePrefs(char *name)
  227. {
  228.     FILE *prefs;
  229.     ULONG subtyp;
  230.  
  231.     if(prefs=fopen(name,"wb"))
  232.     {
  233.         prefs_hdr.id=PREF_FORM;
  234.         prefs_hdr.len=34;
  235.         if(!fwrite(&prefs_hdr,8,1,prefs)) { fclose(prefs);return; }
  236.         subtyp=PREF_PREF;
  237.         if(!fwrite(&subtyp,4,1,prefs)) { fclose(prefs);return; }
  238.  
  239.         prefs_hdr.len=2;
  240.         prefs_hdr.id=PREF_ICAC;
  241.         if(!fwrite(&prefs_hdr,8,1,prefs)) { fclose(prefs);return; }
  242.         if(!fwrite(&d_i,sizeof(d_i),1,prefs)) { fclose(prefs);return; }
  243.  
  244.         prefs_hdr.id=PREF_DCAC;
  245.         if(!fwrite(&prefs_hdr,8,1,prefs)) { fclose(prefs);return; }
  246.         if(!fwrite(&d_d,sizeof(d_d),1,prefs)) { fclose(prefs);return; }
  247.  
  248.         prefs_hdr.id=PREF_CBCK;
  249.         if(!fwrite(&prefs_hdr,8,1,prefs)) { fclose(prefs);return; }
  250.         if(!fwrite(&d_c,sizeof(d_c),1,prefs)) { fclose(prefs);return; }
  251.  
  252.         fclose(prefs);
  253.     }
  254. }
  255.  
  256. void main(int argc,char *argv[])
  257. {
  258.     struct IntuiMessage *imsg;
  259.     ULONG iclass;
  260.     APTR iadr;
  261.     USHORT icode;
  262.     UBYTE quit=0;
  263.     UBYTE mode=0;
  264.  
  265.     if(argc==2 && !stricmp(argv[1],"SET")) mode=1;
  266.     GetSysLevel();
  267.     OpenLibs();
  268.  
  269.     if(mode)
  270.     {
  271.         LoadPrefs("ENV:cc.prefs");
  272.         cachebits=cachemask=0L;
  273.  
  274.         if(syslevel>0)
  275.         {
  276.             cachemask|=(1L|(1L<<4));
  277.             if(d_i.cache) cachebits|=1L;
  278.             if(d_i.burst) cachebits|=(1L<<4);
  279.         }
  280.  
  281.         if(syslevel>1)
  282.         {
  283.             cachemask|=((1L<<8)|(1L<<12));
  284.             if(d_d.cache) cachebits|=(1L<<8);
  285.             if(d_d.burst) cachebits|=(1L<<12);
  286.         }
  287.  
  288.         if(syslevel>2)
  289.         {
  290.             cachemask|=(1L<<31);
  291.             if(d_c.cpybck) cachebits|=(1L<<31);
  292.         }
  293.         CacheControl(cachebits,cachemask);
  294.     }
  295.     else
  296.     {
  297.         OpenGUI();
  298.         while(!quit)
  299.         {
  300.             WaitPort(win->UserPort);
  301.             while(imsg=GT_GetIMsg(win->UserPort))
  302.             {
  303.                 iclass    =imsg->Class;
  304.                 iadr    =imsg->IAddress;
  305.                 icode    =imsg->Code;
  306.                 GT_ReplyIMsg(imsg);
  307.                 switch(iclass)
  308.                 {
  309.                     case IDCMP_GADGETUP:
  310.                         switch((int)(((struct Gadget *)iadr)->GadgetID))
  311.                         {
  312.                             case 0:            /* Save */
  313.                                 SavePrefs("ENVARC:cc.prefs");
  314.                                 SavePrefs("ENV:cc.prefs");
  315.                                 quit=1;
  316.                                 break;
  317.                             case 1:            /* Use */
  318.                                 SavePrefs("ENV:cc.prefs");
  319.                                 quit=1;
  320.                                 break;
  321.                             case 2:            /* Cancel */
  322.                                 quit=1;
  323.                                 break;
  324.  
  325.                             case 10:        /* ICache On/Off */
  326.                                 if(icode) cachebits=cachemask=1L;
  327.                                 else { cachebits=0;cachemask=1L; }
  328.                                 CacheControl(cachebits,cachemask);
  329.                                 break;
  330.                             case 11:        /* IBurst On/Off */
  331.                                 if(icode) cachebits=cachemask=(1L<<4);
  332.                                 else { cachebits=0;cachemask=(1L<<4); }
  333.                                 CacheControl(cachebits,cachemask);
  334.                                 break;
  335.                             case 12:        /* DCache On/Off */
  336.                                 if(icode) cachebits=cachemask=(1L<<8);
  337.                                 else { cachebits=0;cachemask=(1L<<8); }
  338.                                 CacheControl(cachebits,cachemask);
  339.                                 break;
  340.                             case 13:        /* DBurst On/Off */
  341.                                 if(icode) cachebits=cachemask=(1L<<12);
  342.                                 else { cachebits=0;cachemask=(1L<<12); }
  343.                                 CacheControl(cachebits,cachemask);
  344.                                 break;
  345.                             case 14:        /* CopyBack On/Off */
  346.                                 if(icode) cachebits=cachemask=(1L<<31);
  347.                                 else { cachebits=0;cachemask=(1L<<31); }
  348.                                 CacheControl(cachebits,cachemask);
  349.                                 break;
  350.                         }
  351.                         break;
  352.                     case IDCMP_CLOSEWINDOW:
  353.                         quit=1;break;
  354.                 }
  355.             }
  356.         }
  357.     }
  358.     CloseAll();
  359. }
  360.